home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 113 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.2 KB  |  41 lines

  1. Path: news.cs.hope.edu!vnopstal
  2. From: vnopstal@cs.hope.edu (Michael Van Opstall)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: How to make a Complex class
  5. Date: 1 Jan 1996 21:58:19 GMT
  6. Organization: Hope College
  7. Message-ID: <4c9ldr$7e@news.cs.hope.edu>
  8. References: <4c8kdm$p0q@news.csie.nctu.edu.tw>
  9. NNTP-Posting-Host: smaug.cs.hope.edu
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Here's a header for a complex class. I have one completely written. e-mail
  13. me for more info
  14.  
  15. class Complex
  16. {
  17.    friend operator+(Complex c1, Complex c2);
  18.    friend operator-(Complex c1, Complex c2);
  19.    friend operator*(Complex c1, Complex c2);
  20.    friend operator/(Complex c1, Complex c2);
  21.  
  22.    Complex();              // constructs 0+0i
  23.    Complex(double real, double imag);
  24.  
  25.    double mag(Complex c1);    // magnitude of vector
  26.  
  27. private:
  28.    double real, imag;
  29. }
  30.  
  31. That's about all you need for the basics. The only tricky part is the mult
  32. and division operators. The multiplication is just a binomial expansion, but
  33. division is tricky. Hint: you have to multiply by the conjugate. Note that
  34. you'll also need to #include <math.h> for the functions necessary to find a
  35. magnitude.
  36.  
  37.  
  38. --
  39. Michael A. Van Opstall  --  vnopstal@cs.hope.edu
  40. http://www.cs.hope.edu/~vnopstal/deal.html - try it
  41.